
/**
 * @Title: MemberController.java
 * @Package: com.aft.cdcb.modules.member.controller
 * @Description: TODO
 * @author Mandarava
 * @date 2016年9月2日 下午1:33:13
 * @version 1.3.1
 * @Email wuyan1688@qq.com
 */

package com.aft.terra.modules.member.controller;

import org.apache.commons.lang3.StringUtils;

import com.aft.terra.app.Constants;
import com.aft.terra.common.bean.GameBean;
import com.aft.terra.common.bean.Message;
import com.aft.terra.common.interceptor.StartEndDateInterceptor;
import com.aft.terra.common.interceptor.VerificationGameQualificationsInterceptor;
import com.aft.terra.common.utils.CommonUtil;
import com.aft.terra.modules.basic.BaseController;
import com.aft.terra.modules.basic.SystemConfiguration;
import com.aft.terra.modules.member.model.Wechat;
import com.aft.terra.modules.member.service.MemberService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.LogKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;

/**
 * @author Mandarava
 * @date 2016年9月2日 下午1:33:13
 * @version V1.3.1
 */
public class MemberController extends BaseController<Wechat> {

    /**
     * 商城总入口，获取微信授权登陆url
     *
     * @author Mandarava
     */
    @Clear
    public void index() {
        setAttr("url",MemberService.service.weixinOauth2Url(StringUtils.isBlank(getPara("userId")) ? getCookie(Constants.SESSION_NAME) : getPara("userId") ));
        render("/member/login.html");
    }

    /**
     * 微信授权登陆回调
     *
     * @author Mandarava
     */
    @Clear
    public void login() {
        Message message = MemberService.service.weixinLogin(this);
        if (message.isSuccess()) {
            String cookie = shareSession((Wechat) message.getObject(),getPara("state"));
            redirect("/main?userId=" + cookie);
            return;
        }
        redirect("/");
    }
    
    @Clear({StartEndDateInterceptor.class})
    @Before(VerificationGameQualificationsInterceptor.class)
    public void main(){
    	if(StringUtils.isNotBlank(getAttrForStr("uId")) && 
    			getCookie(Constants.SESSION_NAME).equals(getAttrForStr("uId"))){
    		render("index.html");
    	}else{
    		redirect("/main?userId="+getCookie(Constants.SESSION_NAME));
    	}
    }

    /**
     * 获取微信分享需要的参数
     */
    @Clear
    public void getWeixin() {
        GameBean db = new GameBean();
        StringBuffer sb = new StringBuffer();
        sb.append(getPara("url"));
        String url = sb.toString();
        LogKit.info(url +"   +++++++++++");
        ApiConfig apiConifg = SystemConfiguration.getApiConfig();
        ApiConfigKit.setThreadLocalApiConfig(apiConifg);
        String nonceStr = CommonUtil.create_nonce_str();
        String timestamp = CommonUtil.create_timestamp();
        JsTicket jsticket = JsTicketApi.getTicket(JsTicketApi.JsApiType.jsapi);
        String jsapiTicket = jsticket.getTicket();
        String signature = CommonUtil.sign(jsapiTicket, url, nonceStr, timestamp);
        db.setAppId(SystemConfiguration.getWxAppId());
        db.setNonceStr(nonceStr);
        db.setSignature(signature);
        db.setTimestamp(timestamp + "");
        db.setUrl(url);
        renderJson(db);
    }

    @Clear()
    public void loginCaptcha() {
        renderCaptcha();
    }

}
